<template>
  <div>
    <el-container class="home-container">
      <!-- 头 -->
      <el-header>
        <div class="">
          <img src="../assets/heima.png" alt="" />
          <span>电商管理系统</span>
        </div>
        <el-button type="primary" @click="quit">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px':'200px'">
            <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!--  菜单区域 -->
          <el-menu
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <el-submenu :index="item1.id+''" v-for="item1 of menuList" :key="item1.id">
              <template slot="title">
                <i class="iconfont i1" :class="item1.icon"></i>
                <span>{{item1.authName}}</span>
              </template>
              <el-menu-item :index="`/${item2.path}`" v-for="item2 of item1.children" :key="item2.id" @click="changeActiveIndex(`/${item2.path}`)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item2.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容区 -->
        <el-main>
          <!-- 主体区 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
        menuList:[],
        icons:['icon-users','icon-tijikongjian','icon-shangpin','icon-danju','icon-baobiao'],
        isCollapse:false,
        activePath:''
    };
  },
  created() {
    this.getMenuList();
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    quit() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList(){
        const {data:res} = await this.$http.get('/menus')
        if(res.meta.status !==200) return this.$message.error('获取列表失败')
        this.menuList = res.data.map((v,i)=>{
            return{
                ...res.data[i],
                icon:this.icons[i]
            }
        })
        console.log(this.menuList)
    },
    // 点击折叠按钮
    toggleCollapse(){
        this.isCollapse = !this.isCollapse
    },
    // 持久化保存点开的页签
    changeActiveIndex(activePath){
      this.activePath = activePath
      sessionStorage.setItem('activePath',this.activePath)
    }
  },
};
</script>
<style lang="scss" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  padding-left: 0;
  div {
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
      border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
    &.i1{
        margin-right: 10px;
    }
}
.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>